/*******************************************************************************
 * Copyright (c) 2011 Irineu Martins de Lima Moura (imlm2).
 * This file is part of JBeat.
 * 
 *     JBeat is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU Lesser General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 * 
 *     JBeat is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU Lesser General Public License for more details.
 * 
 *     You should have received a copy of the GNU Lesser General Public License
 *     along with JBeat.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * Contributors:
 *     Irineu Martins de Lima Moura (imlm2) - initial API and implementation
 ******************************************************************************/
package br.ufpe.cin.imlm2.jbeat.soot.common;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.regex.Pattern;

import soot.Scene;
import soot.SootClass;

import br.ufpe.cin.imlm2.util.Naming;

public class SootClassResolver {

	/**
	 * Find all the classes in the soot Scene excluding the ones with names in
	 * <code>classesExcludeList</code> or that are in packages or subpackages
	 * with names specified in the <code>packagesExcludeList</code>. The
	 * <code>rtLibraries</code> parameter tells whether the runtime libraries
	 * classes should be included in the resolution or not.
	 * 
	 * @param classesExcludeList
	 * @param packagesExcludeList
	 * @param rtLibraries
	 * @return
	 * @throws NullPointerException
	 *             if any of the list parameters is <code>null</code>.
	 */
	public static Collection<SootClass> resolveClasses(
			List<String> classesExcludeList, List<String> packagesExcludeList,
			boolean rtLibraries) {
		return resolveClasses(new ArrayList<String>(), classesExcludeList,
				packagesExcludeList, rtLibraries);
	}

	/**
	 * Find all the classes in the soot Scene excluding the ones with names in
	 * <code>classesExcludeList</code> or that are in packages or subpackages
	 * with names specified in the <code>packagesExcludeList</code>. The
	 * <code>rtLibraries</code> parameter tells whether the runtime libraries
	 * classes should be included in the resolution or not. The
	 * <code>classesIncludeList</code> parameter specifies explicitly classes
	 * that should be resolved even if they would've been exclude othwerise
	 * (either by being in a package in the <code>packagesExcludeList</code> or
	 * by being in the runtime libraries when the <code>rtLibraries</code>
	 * paramater is false. If a class is in both <code>classesIncludeList</code>
	 * and <code>classesExcludeList</code> it is still going to be resolved.
	 * 
	 * @param classesIncludeList
	 * @param classesExcludeList
	 * @param packagesExcludeList
	 * @param rtLibraries
	 * @return
	 * @throws NullPointerException
	 *             if any of the list parameters is <code>null</code>.
	 */
	public static Collection<SootClass> resolveClasses(
			List<String> classesIncludeList, List<String> classesExcludeList,
			List<String> packagesExcludeList, boolean rtLibraries) {
		Collection<SootClass> allClasses = Scene.v().getClasses();
		Collection<SootClass> appClasses = Scene.v().getApplicationClasses();

		Collection<SootClass> classes = new HashSet<SootClass>();
		List<Pattern> excPackPatterns = createPackagesPatterns(packagesExcludeList);

		for (SootClass sootClass : allClasses) {
			if (classesIncludeList.contains(sootClass.getName())) {
				classes.add(sootClass);
			} else if (!classesExcludeList.contains(sootClass.getName())
					&& !Naming.isTypeInPackageList(sootClass.getName(),
							excPackPatterns)) {
				// If rtLibraries is true then any class should be considered
				// or if the class is in the application classes list then it
				// should also be
				// considered.
				if (rtLibraries || appClasses.contains(sootClass))
					classes.add(sootClass);
			}
		}
		return classes;

	}

	private static List<Pattern> createPackagesPatterns(
			List<String> packagesExcludeList) {
		List<Pattern> packPatterns = new ArrayList<Pattern>();
		for (String s : packagesExcludeList) {
			Pattern p = Naming.createPatternFromPackageName(s);
			if (p != null)
				packPatterns.add(p);
		}
		return packPatterns;
	}

}
